<template>
  <t-row :gutter="[16, 16]">
    <t-col :xs="6" :xl="3">
      <t-card
        title="内存使用"
        :bordered="false"
        :class="{ 'dashboard-item': true, 'dashboard-item--main-color': false }"
      >
        <div class="dashboard-item-top">
          <span :style="{ fontSize: '28px' }">{{ overviewData.memory.used }}</span>
        </div>
        <div class="dashboard-item-left">
          <span :style="{ marginTop: '-24px' }">
            <t-progress theme="circle" :size="'56'" :percentage="overviewData.memory.rate"></t-progress>
          </span>
        </div>
        <template #footer>
          <div class="dashboard-item-bottom">
            <div class="dashboard-item-block">
              {{ '当前系统内存使用情况' }}
            </div>
            <t-icon name="chevron-right" />
          </div>
        </template>
      </t-card>
    </t-col>
    <t-col :xs="6" :xl="3">
      <t-card
        title="代理服务"
        :bordered="false"
        :class="{ 'dashboard-item': true, 'dashboard-item--main-color': false }"
      >
        <div class="dashboard-item-top">
          <span :style="{ fontSize: '28px' }">{{ overviewData.proxyNum }}</span>
        </div>
        <div class="dashboard-item-left">
          <span :style="{ marginTop: '-24px' }">
            <link-icon />
          </span>
        </div>
        <template #footer>
          <div class="dashboard-item-bottom">
            <div class="dashboard-item-block">
              {{ '当前运行的代理服务器数量' }}
            </div>
            <t-icon name="chevron-right" />
          </div>
        </template>
      </t-card>
    </t-col>
    <t-col :xs="6" :xl="3">
      <t-card
        title="接口请求数"
        :bordered="false"
        :class="{ 'dashboard-item': true, 'dashboard-item--main-color': false }"
      >
        <div class="dashboard-item-top">
          <span :style="{ fontSize: '28px' }">{{ overviewData.requestNum }}</span>
        </div>
        <div class="dashboard-item-left">
          <span :style="{ marginTop: '-24px' }">
            <usergroup-icon />
          </span>
        </div>
        <template #footer>
          <div class="dashboard-item-bottom">
            <div class="dashboard-item-block">
              {{ '当前所有代理服务请求总数量' }}
            </div>
            <t-icon name="chevron-right" />
          </div>
        </template>
      </t-card>
    </t-col>
    <t-col :xs="6" :xl="3">
      <t-card
        title="活跃连接数"
        :bordered="false"
        :class="{ 'dashboard-item': true, 'dashboard-item--main-color': false }"
      >
        <div class="dashboard-item-top">
          <span :style="{ fontSize: '28px' }">{{ overviewData.activeNum }}</span>
        </div>
        <div class="dashboard-item-left">
          <span :style="{ marginTop: '-24px' }">
            <sonic-icon />
          </span>
        </div>
        <template #footer>
          <div class="dashboard-item-bottom">
            <div class="dashboard-item-block">
              {{ '当前所有代理服务活跃连接数' }}
            </div>
            <t-icon name="chevron-right" />
          </div>
        </template>
      </t-card>
    </t-col>
  </t-row>
</template>

<script lang="ts">
export default {
  name: 'DashboardBase',
};
</script>

<script setup lang="ts">
import { LinkIcon, SonicIcon, UsergroupIcon } from 'tdesign-icons-vue-next';
import { onMounted, ref } from 'vue';

import { getOverView } from '@/api/home';
import type { OverViewData } from '@/api/model/homeModel';

const overviewData = ref<OverViewData>({
  activeNum: 0,
  memory: {
    free: '0',
    rate: 0,
    total: '0',
    used: '0',
  },
  proxyNum: 0,
  requestNum: 0,
});

onMounted(() => {
  initOverViewData();
});

const initOverViewData = async () => {
  const res = await getOverView();
  overviewData.value = res;
};
</script>

<style lang="less" scoped>
.dashboard-item {
  padding: var(--td-comp-paddingTB-xl) var(--td-comp-paddingLR-xxl);

  :deep(.t-card__header) {
    padding: 0;
  }

  :deep(.t-card__footer) {
    padding: 0;
  }

  :deep(.t-card__title) {
    font: var(--td-font-body-medium);
    color: var(--td-text-color-secondary);
  }

  :deep(.t-card__body) {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    position: relative;
    padding: 0;
    margin-top: var(--td-comp-margin-s);
    margin-bottom: var(--td-comp-margin-xxl);
  }

  &:hover {
    cursor: pointer;
  }

  &-top {
    display: flex;
    flex-direction: row;
    align-items: flex-start;

    > span {
      display: inline-block;
      color: var(--td-text-color-primary);
      font-size: var(--td-font-size-headline-medium);
      line-height: var(--td-line-height-headline-medium);
    }
  }

  &-bottom {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;

    > .t-icon {
      cursor: pointer;
      font-size: var(--td-comp-size-xxxs);
    }
  }

  &-block {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--td-text-color-placeholder);
  }

  &-trend {
    margin-left: var(--td-comp-margin-s);
  }

  &-left {
    position: absolute;
    top: 0;
    right: 0;

    > span {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: var(--td-comp-size-xxxl);
      height: var(--td-comp-size-xxxl);
      background: var(--td-brand-color-light);
      border-radius: 50%;

      .t-icon {
        font-size: 24px;
        color: var(--td-brand-color);
      }
    }
  }

  // 针对第一个卡片需要反色处理
  &--main-color {
    background: var(--td-brand-color);
    color: var(--td-text-color-primary);

    :deep(.t-card__title),
    .dashboard-item-top span,
    .dashboard-item-bottom {
      color: var(--td-text-color-anti);
    }

    .dashboard-item-block {
      color: var(--td-text-color-anti);
      opacity: 0.6;
    }

    .dashboard-item-bottom {
      color: var(--td-text-color-anti);
    }
  }
}
</style>
